#! /usr/bin/env python
#coding=utf-8
import sys
from Tkinter import *


def grid1():
    '''
    简单的例子
    '''
    root=Tk()
    lb1=Label(root, text='hello')
    lb2=Label(root, text='grid')

    lb1.grid()
    lb2.grid()
    root.mainloop()


def grid2():
    '''
    使用行列指定位置
    '''
    root=Tk()
    lb1=Label(root, text='hello')
    lb2=Label(root, text='grid')

    lb1.grid()
    lb2.grid(row=0, column=1)
    root.mainloop()


def grid3():
    '''
    为其它组件预定位置，无法运行，好像挂起了一样
    '''
    root=Tk()
    Label(root, text='hello').pack()
    Label(root, text='grid').grid(row=0,column=10)
    root.mainloop()


def grid4():
    '''
    将组件放置到预定位置上
    '''
    root=Tk()
    Label(root, text='1').grid()
    Label(root, text='2').grid(row=0,column=10)
    Label(root, text='3').grid(row=0,column=5)
    root.mainloop()


def grid5():
    '''
    多个组件在同一位置
    '''
    root=Tk()
    lb1=Label(root, text='1')
    lb2=Label(root, text='2')

    lb1.grid(row=0,column=0)
    lb2.grid(row=0,column=0)

    def forgetLabel():
        print(root.grid_slaves(0,0)[0].grid_forget())

    Button(root, text='forget last',command=forgetLabel).grid(row=1)
    root.mainloop()


def grid6():
    '''
    改变行列属性值
    '''
    root=Tk()
    lb1=Label(root, text='1', bg='red')
    lb2=Label(root, text='2', bg='blue')

    lb1.grid(row=0,column=0)
    lb2.grid(row=0,column=1)

    root.columnconfigure(0,minsize=100)
    root.mainloop()


def grid7():
    '''
    组件使用多列多行
    '''
    root=Tk()
    lbA=Label(root, text='a',bg='red')
    lbB=Label(root, text='b',bg='blue')
    lbC=Label(root, text='c',bg='red')
    lbD=Label(root, text='d',bg='blue')
    lbE=Label(root, text='e',bg='red')

    lbA.grid(row=0,column=0,columnspan=2)
    lbB.grid(row=1,column=0)
    lbC.grid(row=1,column=1)
    lbD.grid(row=2)
    lbE.grid(row=0,column=2)

    root.mainloop()


def grid8():
    '''
    设置表格组件对齐属性
    '''
    root=Tk()
    Label(root, text='hello sticky').grid()
    Label(root, text='tkinter').grid()

    Label(root, text='hello sticky').grid(sticky=W)
    Label(root, text='tkinter').grid(sticky=W)

    root.mainloop()


if __name__=='__main__':
    if sys.argv[1]=='1':
        grid1()
    elif sys.argv[1]=='2':
        grid2()
    elif sys.argv[1]=='3':
        grid3()
    elif sys.argv[1]=='4':
        grid4()
    elif sys.argv[1]=='5':        
        grid5()
    elif sys.argv[1]=='6':
        grid6()
    elif sys.argv[1]=='7':
        grid7()
    elif sys.argv[1]=='8':
        grid8()
